package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.view.ui.adapter.machine.ParentAreaAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class CityPopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private LinearLayout bottomLayout;
    private ParentAreaAdapter parentAreaAdapter;
    private List<ParentAreaEntity> selectedEntities;
    private List<ParentAreaEntity<List<ParentAreaEntity>>> areaEntityList;
    private String no = "";
    private String name = "";

    public CityPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_area_two;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        bottomLayout = holder.getPop().findViewById(R.id.bottom_layout);
        okView = holder.getPop().findViewById(R.id.tv_ok);
        resetView = holder.getPop().findViewById(R.id.tv_next);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(no, selectedEntities,name);
                    Log.d("城市编号", no);
                }
            }
        });

        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomLayout.setVisibility(View.GONE);
                recyclerView.setAdapter(parentAreaAdapter);
                parentAreaAdapter.setDatas(areaEntityList);
            }
        });
        parentAreaAdapter = new ParentAreaAdapter(mContext, null);
        parentAreaAdapter.setItemSelect(true);
        parentAreaAdapter.setDefItem(-1);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(parentAreaAdapter);
        parentAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity<List<ParentAreaEntity>>>() {
            @Override
            public void onItemClick(ParentAreaEntity<List<ParentAreaEntity>> listParentAreaEntity, int position) {
                /*if (no != listParentAreaEntity.getNo() && !"".equals(no)) {
                    childAreaAdapter.resetCheckDatas();
                }*/
                no = listParentAreaEntity.getNo();
                name = listParentAreaEntity.getName();
                selectedEntities = listParentAreaEntity.getChildren();
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(no, selectedEntities,name);
                    Log.d("城市编号", no);
                }
            }
        });
    }

    public void setData(List<ParentAreaEntity<List<ParentAreaEntity>>> areaEntityList, boolean isChange) {
        this.areaEntityList = areaEntityList;
        parentAreaAdapter.setDatas(areaEntityList);
        if (isChange) {
            bottomLayout.setVisibility(View.GONE);
            parentAreaAdapter.resetCheckDatas();
            recyclerView.setAdapter(parentAreaAdapter);
        }
    }
}
